package com.ruihang.ijkplaylib.widget;

import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruihang.ijkplaylib.R;
import com.ruihang.ijkplaylib.utils.PlayerUtils;

import java.lang.ref.WeakReference;

import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by rh008 on 2017/7/20.
 */

public class GPlayerScreenControllImp extends AbsSreenControll {

    protected WeakReference<AppCompatActivity> activityWeak;

    protected ViewGroup volumeBox;
    protected ViewGroup brightBox;
    protected ViewGroup fastForwardBox;

    protected ViewGroup loadBox;

    protected ImageView volumeIcon;
    protected ImageView brightIcon;
    protected TextView volumeTx;
    protected TextView brightTx;

    protected TextView fastForwardAll;
    protected TextView fastForwardTarget;
    protected TextView fastForward;

    protected ProgressBar progressBar;
    protected TextView speed;

    protected boolean isShowSpeed = true;

    public GPlayerScreenControllImp(AppCompatActivity activity) {
        activityWeak = new WeakReference<AppCompatActivity>(activity);
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        View v = inflater.inflate(R.layout.simple_player_touch_gestures, group, false);
        volumeBox = (ViewGroup) v.findViewById(R.id.app_video_volume_box);
        volumeIcon = (ImageView) v.findViewById(R.id.app_video_volume_icon);
        volumeTx = (TextView) v.findViewById(R.id.app_video_volume);
        brightBox = (ViewGroup) v.findViewById(R.id.app_video_brightness_box);
        brightIcon = (ImageView) v.findViewById(R.id.app_video_brightness_icon);
        brightTx = (TextView) v.findViewById(R.id.app_video_brightness);
        fastForwardBox = (ViewGroup) v.findViewById(R.id.app_video_fastForward_box);
        fastForwardAll = (TextView) v.findViewById(R.id.app_video_fastForward_all);
        fastForwardTarget = (TextView) v.findViewById(R.id.app_video_fastForward_target);
        fastForward = (TextView) v.findViewById(R.id.app_video_fastForward);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams
                .WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        group.addView(v, lp);
        AppCompatActivity ac = getActivityTemp();
        if (ac != null) {
            v = inflater.inflate(R.layout.loading_player, group, false);
            progressBar = (ProgressBar) v.findViewById(R.id.loading_progressbar);
            speed = (TextView) v.findViewById(R.id.loading_speed);
            lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            group.addView(v, lp);
            loadBox = (ViewGroup) v;
            if (isShowSpeed) {
                speed.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public boolean isVolumeArea(float x, int screenWidth) {
        return x > screenWidth * 0.5f;
    }

    @Override
    public void onBrightnessSlide(float persent) {
        volumeBox.setVisibility(View.GONE);
        brightBox.setVisibility(View.VISIBLE);
        brightTx.setText(((int) (persent * 100)) + "%");
    }

    @Override
    public void onVolumeSlide(int i) {
        String s = i + "%";
        if (i == 0) {
            s = "off";
        }
        volumeIcon.setImageResource(i == 0 ? R.drawable.simple_player_volume_off_white_36dp : R.drawable.simple_player_volume_up_white_36dp);
        brightBox.setVisibility(View.GONE);
        volumeBox.setVisibility(View.VISIBLE);
        volumeTx.setText(s);
    }

    @Override
    public void onProgressSlide(long delta, long duration, long pos) {
        int showDelta = (int) delta / 1000;
        if (showDelta != 0) {
            fastForwardBox.setVisibility(View.VISIBLE);
            String text = showDelta > 0 ? ("+" + showDelta) : "" + showDelta;
            fastForward.setText(text + "s");
            fastForwardTarget.setText(PlayerUtils.generateTime(pos) + "/");
            fastForwardAll.setText(PlayerUtils.generateTime(duration));
        }
    }

    @Override
    public void showStatus(String text) {
    }

    @Override
    public void onProgressChanged(boolean isShow) {
        if (loadBox != null) {
            loadBox.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onShowSome(int mode) {
    }

    @Override
    public void onSetSome(int mode, Message msg) {
//        msg.recycle();
    }

    @Override
    public void endGesture() {
        volumeBox.setVisibility(View.GONE);
        brightBox.setVisibility(View.GONE);
        fastForwardBox.setVisibility(View.GONE);
    }

    @Override
    public boolean hasBarrage() {
        return false;
    }

    @Override
    public boolean onShowPanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        //一般情况下需要隐藏中间菜单，分辨率等
        return true;
    }

    @Override
    public void onHidePanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
    }

    @Override
    public void onPortrait() {

    }

    @Override
    public void onLandscape() {

    }

    @Override
    public void onDetach(ViewGroup group) {
    }

    @Override
    public void onDestory() {
    }

    @Override
    public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
        if (what == PlayStateParams.MEDIA_INFO_NETWORK_BANDWIDTH || what == PlayStateParams.MEDIA_INFO_BUFFERING_BYTES_UPDATE) {
//            Log.e("dou361", "====extra=======" + extra);
            if (speed != null) {
                speed.setText(PlayerUtils.getFormatSize(extra));
            }
        }
        return false;
    }

    protected AppCompatActivity getActivityTemp() {
        if (activityWeak != null) {
            return activityWeak.get();
        }
        return null;
    }

    public GPlayerScreenControllImp setShowSpeed(boolean showSpeed) {
        boolean b = this.isShowSpeed != showSpeed;
        isShowSpeed = showSpeed;
        if (b && speed != null) {
            speed.setVisibility(isShowSpeed ? View.VISIBLE : View.GONE);
        }
        return this;
    }
}
